import 'package:flutter/material.dart';
import 'package:flutter_shop/components/home/ad_banner.dart';
import 'package:flutter_shop/components/home/floor_content.dart';
import 'package:flutter_shop/components/home/hot_goods_area.dart';
import 'package:flutter_shop/components/home/leader_phone.dart';
import 'package:flutter_shop/components/home/recommend_goods.dart';
import 'package:flutter_shop/components/home/navigator_list.dart';
import 'package:flutter_shop/components/home/swiper_diy.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import '../model/home_page_data.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  int _page = 1;
  var _hotGoods;
  EasyRefreshController _refreshController;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    _refreshController = EasyRefreshController();
    _hotGoods = HotGoodsData['hotGoods'];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Future<void> _onRefresh() async{
      return Future.delayed(Duration(seconds: 1), () {
        _refreshController.resetLoadState();
      });
    }

    Future<void> _onLoadMore () async {
      return Future.delayed(Duration(seconds: 1), () {
        // _hotGoods['list'].addAll(HotGoodsData['hotGoods']['list']);
        _page++;
        _refreshController.finishLoad(noMore: _page >= 4);
      });
    }

    if (_hotGoods != null) {
      return Scaffold(
        appBar: AppBar(
          title: Text('百姓生活+'),
          elevation: 0.0
        ),

        body: EasyRefresh(
          controller: _refreshController,
          child: ListView(
            children: <Widget>[
              SwiperDiy(
                swiperDataList: HomePageSwiperImgs
              ),
              NavigatorList(
                navigatorList: NavigatorListData
              ),
              AdBanner(adPicture: AdPicture),
              LeaderPhone(leaderPhone: '18681169356', leaderImage:LeaderImage),
              RecommendGoods(recommendGoods: RecommendList),
              FloorContent(floorData: FloorData['floor1']),
              HotGoodsArea(hotGoodsData: _hotGoods),
            ]
          ),

          onRefresh: _onRefresh,

          onLoad: _onLoadMore
        )
      );
    } else {
      return Center(
        child: Text('加载中...')
      );
    }
  }
}